﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Collection of <see cref="DigitalQuran.WordPart"/>
    /// </summary>
    public class WordPartCollection : ReadOnlyCollection<WordPart>
    {
        internal WordPartCollection() : base(0) { }
        
        public override WordPart this[int index]
        {
            get
            {
                if (index < Count)
                    return _WordPart[index];
                else
                    throw new IndexOutOfRangeException();
            }
        }

        public override IEnumerator<WordPart> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return _WordPart[i];
            }
        }

        WordPart[] _WordPart = null;

        internal void ReadFromStream(Word word, BinaryReader readerStream)
        {
            int count = readerStream.ReadInt32();
            _WordPart = new WordPart[count];
            this.Count = count;

            for (int i = 1; i <= count; i++)
            {
                _WordPart[i - 1] = new WordPart(word, i, ref readerStream);
            }
        }
    }
}